home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3 / CHAPTER8 / CFGDTSRC.C < prev    next >
C/C++ Source or Header  |  1996-04-28  |  7KB  |  218 lines

  1.  
  2. #include <windows.h>  
  3. #include <stdio.h>
  4. #include "cfgdtsrc.h"
  5. #include "sqlext.h"  
  6. #include "odbcinst.h"  
  7.  
  8.  
  9. #if defined (WIN32)
  10.     #define IS_WIN32 TRUE
  11. #else
  12.     #define IS_WIN32 FALSE
  13. #endif
  14.  
  15. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  16. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  17. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  18.  
  19. HINSTANCE hInst;   // current instance
  20.  
  21. LPCTSTR lpszAppName = "MyApp";
  22. LPCTSTR lpszTitle   = "SQLConfigDataSource()"; 
  23.  
  24.  
  25. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  26.  
  27.  
  28. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  29.                       LPTSTR lpCmdLine, int nCmdShow)
  30. {
  31.    MSG      msg;
  32.    HWND     hWnd; 
  33.    WNDCLASS wc;
  34.  
  35.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  36.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  37.    wc.cbClsExtra    = 0;                      
  38.    wc.cbWndExtra    = 0;                      
  39.    wc.hInstance     = hInstance;              
  40.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  41.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  42.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  43.    wc.lpszMenuName  = lpszAppName;              
  44.    wc.lpszClassName = lpszAppName;              
  45.  
  46.    if ( IS_WIN95 )
  47.    {
  48.       if ( !RegisterWin95( &wc ) )
  49.          return( FALSE );
  50.    }
  51.    else if ( !RegisterClass( &wc ) )
  52.       return( FALSE );
  53.  
  54.    hInst = hInstance; 
  55.  
  56.    hWnd = CreateWindow( lpszAppName, 
  57.                         lpszTitle,    
  58.                         WS_OVERLAPPEDWINDOW, 
  59.                         CW_USEDEFAULT, 0, 
  60.                         CW_USEDEFAULT, 0,  
  61.                         NULL,              
  62.                         NULL,              
  63.                         hInstance,         
  64.                         NULL               
  65.                       );
  66.  
  67.    if ( !hWnd ) 
  68.       return( FALSE );
  69.  
  70.    ShowWindow( hWnd, nCmdShow ); 
  71.    UpdateWindow( hWnd );         
  72.  
  73.    while( GetMessage( &msg, NULL, 0, 0) )   
  74.    {
  75.       TranslateMessage( &msg ); 
  76.       DispatchMessage( &msg );  
  77.    }
  78.  
  79.    return( msg.wParam ); 
  80. }
  81.  
  82.  
  83. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  84. {
  85.    WNDCLASSEX wcex;  
  86.  
  87.    wcex.style         = lpwc->style;
  88.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  89.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  90.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  91.    wcex.hInstance     = lpwc->hInstance;
  92.    wcex.hIcon         = lpwc->hIcon;
  93.    wcex.hCursor       = lpwc->hCursor;
  94.    wcex.hbrBackground = lpwc->hbrBackground;
  95.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  96.    wcex.lpszClassName = lpwc->lpszClassName;
  97.  
  98.    // Added elements for Windows 95.
  99.    //...............................
  100.    wcex.cbSize = sizeof(WNDCLASSEX);
  101.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  102.                             IMAGE_ICON, 16, 16,
  103.                             LR_DEFAULTCOLOR );
  104.             
  105.    return RegisterClassEx( &wcex );  
  106. }
  107.  
  108.  
  109. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  110. {
  111. static HWND  hList          = NULL;
  112. static UCHAR pszDriver[]    = "Microsoft Access Driver (*.mdb)";
  113. static UCHAR szAttributes[] = "DSN=Test Data Source\0\0";
  114.  
  115.    switch( uMsg )
  116.    {
  117.       case WM_CREATE :
  118.               {
  119.                  hList = CreateWindow( "LISTBOX", "",    
  120.                                        LBS_NOTIFY | WS_VSCROLL | 
  121.                                        WS_BORDER  | WS_CHILD | 
  122.                                        WS_VISIBLE | LBS_NOINTEGRALHEIGHT, 
  123.                                        0, 0, 
  124.                                        0, 0,  
  125.                                        hWnd,              
  126.                                        (HMENU)101,              
  127.                                        hInst,         
  128.                                        NULL               
  129.                                      );
  130.               }
  131.               break;
  132.  
  133.       case WM_SIZE :
  134.               MoveWindow( hList, 0, 0, LOWORD( lParam ), 
  135.                                        HIWORD( lParam ), TRUE );
  136.               break; 
  137.               
  138.       case WM_COMMAND :
  139.               switch( LOWORD( wParam ) )
  140.               {
  141.                  case IDM_TEST :
  142.                         {
  143.                            BOOL  bRet;
  144.                            UCHAR szLBStr[128];
  145.  
  146.                            // Call SQLConfigDataSource() to 
  147.                            // configure "Test Data Source".
  148.                            // .............................
  149.                            bRet = SQLConfigDataSource( hWnd, 
  150.                                      ODBC_CONFIG_DSN, 
  151.                                      pszDriver, szAttributes );
  152.  
  153.                            strcpy( szLBStr, "SQLConfigDataSource() " );
  154.  
  155.                            // Display status to the user.
  156.                            // ...........................
  157.                            if( bRet )
  158.                            {
  159.                               strcat( szLBStr, "successful" );
  160.  
  161.                               SendMessage( hList, LB_ADDSTRING, 0, 
  162.                                           (LPARAM)szLBStr );
  163.                            }
  164.                            else
  165.                            {
  166.                               strcat( szLBStr, "not successful" );
  167.  
  168.                               SendMessage( hList, LB_ADDSTRING, 0, 
  169.                                           (LPARAM)szLBStr );
  170.                            }
  171.                         }
  172.                         break;
  173.  
  174.                  case IDM_ABOUT :
  175.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  176.                         break;
  177.  
  178.                  case IDM_EXIT :
  179.                         DestroyWindow( hWnd );
  180.                         break;
  181.               }
  182.               break;
  183.       
  184.       case WM_DESTROY :
  185.               PostQuitMessage(0);
  186.               break;
  187.  
  188.       default :
  189.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  190.    }
  191.  
  192.    return( 0L );
  193. }
  194.  
  195.  
  196. LRESULT CALLBACK About( HWND hDlg,           
  197.                         UINT message,        
  198.                         WPARAM wParam,       
  199.                         LPARAM lParam)
  200. {
  201.    switch (message) 
  202.    {
  203.        case WM_INITDIALOG: 
  204.                return (TRUE);
  205.  
  206.        case WM_COMMAND:                              
  207.                if (   LOWORD(wParam) == IDOK         
  208.                    || LOWORD(wParam) == IDCANCEL)    
  209.                {
  210.                        EndDialog(hDlg, TRUE);        
  211.                        return (TRUE);
  212.                }
  213.                break;
  214.    }
  215.  
  216.    return (FALSE); 
  217. }
  218.